﻿/*
	Copyright (c) 第三界主 2014
	代码使用MIT协议发布
*/
#ifndef WAYLAND_HELLO_H
#define WAYLAND_HELLO_H


#include <wayland-client.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/input.h>


/* 某些struct wl_开头的对象实际上是wl_proxy对象的id，与windows下的Handle类似。 */
typedef struct wl_display* HDISPLAY;
typedef struct wl_registry* HREGISTRY;
typedef struct wl_compositor* HCOMPOSITOR;
typedef struct wl_shell* HSHELL;
typedef struct wl_shm* HSHM;
typedef struct wl_shm_pool* HSHMPOOL;
typedef struct wl_seat* HSEAT;
typedef struct wl_pointer* HPOINTER;
typedef struct wl_surface* HSURFACE;
typedef struct wl_shell_surface * HSHELLSURFACE;
typedef struct wl_callback* HCALLBACK;
typedef struct wl_buffer* HBUFFER;
typedef struct wl_keyboard* HKEYBOARD;
typedef void* LPVOID;

typedef struct _WAYLANDREGISTRY
{
	/* 所有API都需要wl_display */
	HDISPLAY s_display;

	/* 合成器 */
	HCOMPOSITOR s_compositor;

	/* shell提供接口，与窗口操作有关 */
	HSHELL s_shell;

	/* 缓存管理器，与绘制相关 */
	HSHM s_shm;
	uint32_t shm_formats;

	/* 输入设备管理器，管理键盘鼠标 */
	HSEAT s_seat;

	/* 鼠标控制器 */
	HPOINTER s_pointer;

	/* 键盘控制器 */
	HKEYBOARD s_keyboard;

	/* 鼠标当前窗口 */
	HSURFACE s_pointer_surface;

	/* 键盘当前窗口 */
	HSURFACE s_kb_surface;

} WAYLANDREGISTRY, *LPWAYLANDREGISTRY;

/* wayland的全局对象都放在registry中 */
LPWAYLANDREGISTRY wlGetRegistry();

typedef struct _PAINTBUFFER
{
	HBUFFER buffer;
	void *data;
	int busy;
}PAINTBUFFER, *LPPAINTBUFFER;


typedef struct _PAINTSTRUCTURE
{
	LPPAINTBUFFER buffer;
} PAINTSTRUCTURE, *LPPAINTSTRUCTURE;

typedef void(*REPAINTPROC)(HSURFACE hSurface, LPPAINTSTRUCTURE lpPaint, uint32_t time);

typedef struct _SURFACEPRIVATE
{
	struct wl_surface *surface;
	struct wl_shell_surface *shell_surface;
	int width, height;
	REPAINTPROC surfaceproc;
	PAINTBUFFER buffer[2];
} SURFACEPRIVATE, *LPSURFACEPRIVATE;

typedef struct _SIZE
{
	int width, height;
} SIZE;

typedef int POINTERPROC;
typedef int KEYBOARDPROC;

static inline HDISPLAY wlGetDisplay() 
{
	return wlGetRegistry()->s_display;
}

static inline HCOMPOSITOR wlGetCompositor()
{
	return wlGetRegistry()->s_compositor;
}

static inline HSHELL wlGetShell()
{
	return wlGetRegistry()->s_shell;
}

static inline HPOINTER wlGetPointer()
{
	return wlGetRegistry()->s_pointer;
}

static inline HKEYBOARD wlGetKeyboard()
{
	return wlGetRegistry()->s_keyboard;
}

LPSURFACEPRIVATE _wlGetSurfacePrivate(HSURFACE surface);
void _wlCreatePaintStructure(int width, int height, LPPAINTBUFFER lpBuffer);
void _wlSetFrameListener(HSURFACE surface);
void _wlDrawFrame(HSURFACE surface, uint32_t time);
void wlBeginPaint(HSURFACE surface, LPPAINTSTRUCTURE lpPaint);
void wlEndPaint(HSURFACE surface, LPPAINTSTRUCTURE lpPaint);

int wlInit();
void wlTerminate();

HSURFACE wlCreateSurface(const char* title, int width, int height);
void wlSetPaintProc(HSURFACE surface, REPAINTPROC surfaceproc);
void wlCallPaintProc(HSURFACE hSurface, LPPAINTSTRUCTURE lpPaint, uint32_t time);
void wlSetPointerProc(HSURFACE surface, POINTERPROC surfaceproc);
void wlSetKeyboardProc(HSURFACE surface, KEYBOARDPROC surfaceproc);
SIZE wlGetSurfaceSize(HSURFACE surface);
int wlDispatch();

#endif
